package main

import (
	"database/sql"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
	"math/rand"
	"strconv"
	"time"
)

import _ "github.com/go-sql-driver/mysql"

func Rand1(max int) int {
	rand.Seed(time.Now().UnixNano())
	return rand.Intn(max)
}
func main() {
	r := gin.Default()

	r.GET("/:id", func(context *gin.Context) {
		sid := context.Param("id")
		log.Println(sid)
		id, _ := strconv.Atoi(sid)
		db2, e := sql.Open("mysql", "server:123@tcp(118.24.238.198)/test")
		db2.SetMaxOpenConns(2)
		defer db2.Close()
		if nil != e {
			log.Print(e.Error())
		}
		tx, _ := db2.Begin()
		rows, e := tx.Query("select test from test where id = 1 for update ")
		if nil != e {
			log.Print(e.Error())
		}
		rows.Next()
		test := ""
		e = rows.Scan(&test)
		if nil != e {
			log.Print(e.Error())
		}
		e = rows.Close()
		if nil != e {
			log.Print(e.Error())
		}
		t := []int32{}
		json.Unmarshal([]byte(test), &t)
		t = append(t, int32(id))
		b1, _ := json.Marshal(t)
		_, e = tx.Exec("update test set test = " + "'" + string(b1) + "'" + "where id = 1")
		tx.Commit()
		if nil != e {
			log.Print(e.Error())
		}

	})
	r.Run(":8950")

}
